﻿#include <vector>
#include <list>
#include <pthread.h>
#include "SendThreadPool.h"

CSendThreadPool* CSendThreadPool::pPoolObj = nullptr;

std::mutex CSendThreadPool::mutex_;

CSendThreadPool::GC  CSendThreadPool::gc;

CSendThreadPool::CSendThreadPool()
{
	m_ThreadObjNum = 0 ;
	
}
    
CSendThreadPool::~CSendThreadPool()
{
	
}
bool CSendThreadPool::Start(int ThreadObjNum)
{
	m_ThreadObjNum = ThreadObjNum;
	for(int i = 0; i < ThreadObjNum; ++i)
    {
        CSendThread *pThreadObj = new CSendThread;

		pThreadObj->Start(i);

        m_ThreadVector.push_back(pThreadObj);
    }
	return  true;
}
void CSendThreadPool::Stop()
{
    for(int i = 0; i < m_ThreadObjNum; ++i)
    {
		m_ThreadVector[i]->Stop();

        delete m_ThreadVector[i];
    }
    m_ThreadVector.clear();
}
void CSendThreadPool::AddTask(int nID, char  *szTask)
{
    //printf("AddTask SendTask:%d********************\n", nID);

	m_ThreadVector[nID]->AddTask(szTask);
}	
size_t CSendThreadPool::SendProc(int nID, void* pClient, char* data, int  nLen)
{
    
    return  (m_ThreadVector[nID]->SendProc((sClient*)pClient,data, nLen));

}
void  CSendThreadPool::SemPost(int  nID)
{
    if (sem_post(&m_ThreadVector[nID]->m_semEventSendQueue) == -1)
    {
        printf("Send  thread sem_post error %d \n", nID );
    }
}
CSendThreadPool* CSendThreadPool::GetObject()
{
	static CSendThreadPool cPool;

	return &cPool;
}
CSendThreadPool* CSendThreadPool::GetInstance()
{
    if (pPoolObj == nullptr) //指针为空，则创建管理器
    {
        std::lock_guard<std::mutex> lock(mutex_);
        if (pPoolObj == nullptr) //双重检测
        {
            pPoolObj = new CSendThreadPool();
        }
    }
    return pPoolObj;
}